package com.ruoyi.system.mapper;

import com.ruoyi.system.domain.Story;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.SysPost;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

public interface StoryMapper {
    /**
     * 根据id查询studentId
     * @param id
     * @return
     */
    @Select("select studentId from story where id = #{id}")
    int getStudentIdById(int id);

    /**
     * 新增小事说
     * @param story
     * @return
     */
    @Insert("insert into story(studentId,problem,programme,pic,remarks,referState,date) values(#{studentId},#{problem},#{programme},#{pic},#{remarks},#{referState},#{date})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")/*增加这个注解插入记录后会返回自增长的id*/
    public int insertStory(Story story);

    @Select("select * from story where studentId like concat('%', #{studentId}, '%') and classID like concat('%', #{classID}, '%') and departmentID like concat('%', #{departmentID}, '%') order by date desc")
    List<Story> selectStoryListBy(String studentId, String classID, String departmentID);

    @Select("select * from story where studentId = #{studentId} order by date desc")
    List<Story> selectStoryListByStudentId(String studentId);

    @Select("select * from story where classId = #{classID} order by date desc")
    List<Story> selectStoryListByClassID(String classID);


    @Select("select s.id, s.studentId, s.problem, s.programme, s.remarks, s.date, s.mychatcount, s.handleState from story s right join story_sysemployee ss on s.id = ss.storyId  where ss.departmentId = #{departmentID} group by ss.storyId order by date desc ")
    List<Story> selectStoryListByDepartmentID(String departmentID);


    @Select("select * from story order by date desc")
    List<Story> selectStoryList();

    @Select("select * from story where date(date) between #{ beginTime } and #{ endTime } order by date desc")
    List<Story> selectStoryListByTime(@Param("beginTime") String beginTime, @Param("endTime") String endTime);

    /**
     * 通过小事ID查询岗位信息
     *
     * @param id 小事ID
     * @return 角色对象信息
     */
    @Select("select * from story where id = #{id}")
    public Story selectStoryById(Long id);

    /**
     * 批量删除小事信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteStoryByIds(Long[] ids);

    /**
     * 批量删除小事信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteStorySByIds(Long[] ids);

    /**
     * 批量删除小事信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteMessageByIds(Long[] ids);

    /**
     * 根据id查询小事的状态
     */
    @Select("select * from story where id = #{id}")
    public Story selectStateById(int id);

    /**
     * 根据id更改进度处理状态
     * @param id
     * @param handleState
     * @return
     */
    @Update("update story set handleState = #{ handleState } where id = #{ id }")
    public int updateHandleState(@Param("id") int id, @Param("handleState") int handleState);

    /**
     * 设置为无效状态
     * @param id
     * @return
     */
    @Update("update story set handleState = -1 where id = #{ id }")
    public int invalidHandleState(@Param("id") int id);

    /**
     * 小事说的未读浏览信息数+1
     * @param id
     * @return
     */
    @Update("update story set mychatcount=mychatcount+1 where id = #{id}")
    int updateStatusCount(@Param("id") int id);

    /**
     * 小事说的未读浏览信息数置为0
     * @param id
     * @return
     */
    @Update("update story set mychatcount=0 where id = #{id}")
    int updateMychatcountZero(@Param("id") int id);

    /**
     * 查询所有未读聊天总数
     * @return
     */
    @Select("select sum(mychatcount) from story")
    int selectSumChatCount();


    /**
     * 根据学生id搜索所有提交的小事说
     * @param studentId
     * @return
     */
    @Select("select * from story where studentId = #{studentId} order by date desc")
    List<Story> selectStroyListByStudentId(@Param("studentId") int studentId);


    @Update("update story set handleState = #{ handleState }, handleTime = #{ handleTime } where id = #{ id }")
    public int updateHandleStateAndHandleTime(@Param("id") int id, @Param("handleState") int handleState, @Param("handleTime") String handleTime);

    /**
     * 根据id更改小事分类
     * @param id
     * @return
     */
    @Update("update story set classId = #{ classId } where id = #{ id }")
    public int updateClassId(@Param("id") int id, @Param("classId") int classId);


    /**
     * 根据id查询分类id
     * @param id
     * @return
     */
    @Select("select classId from story where id = #{id}")
    int selectClassById(@Param("id") int id);

    /**
     * 根据id查询时间
     * @return
     */
    @Select("select handleTime from story where id = #{id}")
    public String selectHandleTimeById(@Param("id") int id);

    /**
     * 根据小事id和学生id 查询图片地址
     * @param storyId
     * @return
     */
    @Select("select pic from story where id = #{id}")
    public String getHeadImgUrlByStoryIdAndStudentId(@Param("id") int storyId);


    @Update("update story set opinion = #{ opinion }, opinionTime = #{ opinionTime } where id = #{ id }")
    int updateOpinion(@Param("id") int id, @Param("opinion") String opinion, @Param("opinionTime") Date opinionTime);

    @Update("update story set handleState = #{ handleState }, handleTime = #{ handleTime }, opinion = #{ opinion }, opinionTime = #{ opinionTime }  where id = #{ id }")
    int updateHandleStateAndHandleTimeAndOpinion(@Param("id") int id, @Param("handleState") int handleState, @Param("handleTime") String handleTime, @Param("opinion") String opinion, @Param("opinionTime") Date opinionTime);

    @Select("select opinion from story where id = #{id}")
    Story selectStoryOpinion(int id);

    /**
     * 根据id更新强制提交的次序
     * @param storyId
     * @param forcedSubmitOrder
     * @return
     */
    @Update("update story set isForcedSubmit = #{ isForcedSubmit }, forcedSubmitTime = #{ forcedSubmitTime } where id = #{ id }")
    int updateIsForcedSubmitById(@Param("id") int storyId, @Param("isForcedSubmit") int forcedSubmitOrder, @Param("forcedSubmitTime") Date forcedSubmitTime);

    /**
     * 根据id查询强制提交时间
     * @param storyId
     * @return
     */
    @Select("select forcedSubmitTime from story where id = #{id}")
    Date selectForcedSubmitTime(@Param("id") int storyId);

    /**
     * 查询所有小事
     * @return
     */
//    @Select("select id, problem, programme, remarks, classId, date, handleState from story")
    List<Story> selectStorys(Long[] ids);

}